<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>校验属性值</title>
</head>
<body>
<script>
    function Ninja() {
        let _skillLevel = 0;
        Object.defineProperty(this, 'skillLevel', {
            get: () => _skillLevel,
            set: value => {
                if (!Number.isInteger(value)) {
                    throw new TypeError('skill level must be a number');
                }
                _skillLevel = value;
            }
        })
    }
    const ninja = new Ninja();
    ninja.skillLevel = 10;
    try {
        ninja.skillLevel = 'aa';
    }catch (e) {
        console.error(e);
    }
</script>
</body>
</html>
